<!DOCTYPE html>
<html lang="zh_CN">
<head>
    <#assign currentMenu="goods"/>
    <#include "../common/header.ftl">
    <script src="/js/plugins/jquery-bootstrap/jquery.bootstrap.min.js"></script>
    <script src="/js/plugins/jquery-form/jquery.form.js"></script>
    <style type="text/css">
        .modal-body {
            max-height: 750px;
            overflow-y: auto;
        }

        .modal-body img {
            width: 100%;
        }
    </style>
</head>
<body>

<div class="container " style="margin-top: 20px">
    <#include "../common/top.ftl">
    <div class="row">
        <div class="col-lg-3">
            <#include "../common/menu.ftl">
        </div>
        <div class="col-lg-9">
            <div class="row">
                <div class="col-lg-12">
                    <h1 class="page-head-line">商品管理</h1>
                </div>
            </div>

            <form class="form-inline" id="searchForm" action="/goods/list.do" method="post">
                <input type="hidden" name="currentPage" id="currentPage" value="1">
                <div class="form-group">
                    <label>关键字:</label>
                    <input type="text" class="form-control" name="keyword" value="${(qo.keyword)!}"
                           placeholder="请输入商品名称">
                </div>
                <button class="btn btn-info">
                    <span class="glyphicon glyphicon-search"></span> 查询
                </button>
                <a type="button" class="btn btn-success inputBtn">
                    <span class="glyphicon glyphicon-plus"></span> 新增
                </a>

            </form>

            <table class="table table-striped table-hover">
                <tr>
                    <th>序号</th>
                    <th>封面</th>
                    <th>商品名称</th>
                    <th>价格(积分)</th>
                    <th>库存</th>
                    <th>描述</th>
                    <th>上架时间</th>
                    <th>类别</th>
                    <th>状态</th>
                    <th>操作</th>
                </tr>
                <#list result.list as entity>
                    <tr>
                        <td>${entity_index + 1}</td>
                        <td><img src="${entity.coverUrl}" width="100"/></td>
                        <td>${entity.title}</td>
                        <td>${entity.price}</td>
                        <td>${entity.inventory}</td>
                        <td>${entity.description}</td>
                        <td>${entity.releaseTime?string("yyyy-MM-dd")}</td>
                        <td>${entity.type.name}</td>
                        <td>${entity.stateName}</td>
                        <td>
                            <a type="button" class="btn btn-primary btn-xs deleteBtn"
                               data-id='${entity.id}'>
                                <span class="glyphicon glyphicon-pencil"></span> 删除
                            </a>
                            <a type="button" class="btn btn-primary btn-xs inputBtn"
                               data-json='${entity.json}'>
                                <span class="glyphicon glyphicon-pencil"></span> 修改
                            </a>

                        </td>
                    </tr>
                </#list>
            </table>
            <#include "../common/page.ftl">
        </div>
    </div>
</div>

<#-- strategy -->
<div id="inputModal" class="modal" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title">编辑</h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal" action="/goods/saveOrUpdate.do" method="post"
                      enctype="multipart/form-data" id="editForm">
                    <input type="hidden" name="id"/>
                    <div class="form-group" >
                        <label class="col-lg-4 control-label">商品名称：</label>
                        <div class="col-lg-6">
                            <input type="text" class="form-control" name="title"/>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-lg-4 control-label">价格(积分)：</label>
                        <div class="col-lg-6">
                            <input type="text" class="form-control" name="price"/>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-lg-4 control-label">封面：</label>
                        <div class="col-lg-6">
                            <img id="coverUrl" style="width: 100%"/>
                            <input type="file" class="form-control" name="img"/>
                            <input type="hidden" name="coverUrl"/>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-lg-4 control-label">库存：</label>
                        <div class="col-lg-6">
                            <input type="text" class="form-control" name="inventory"/>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-lg-4 control-label">描述：</label>
                        <div class="col-lg-6">
                            <input type="text" class="form-control" name="description"/>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-lg-4 control-label">类别：</label>
                        <div class="col-lg-6">
                            <select class="form-control" name="type.id">
                                <#list types as r>
                                    <option value="${r.id}">${r.name}</option>
                                </#list>
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-lg-4 control-label">状态：</label>
                        <div class="col-lg-6">
                            <select class="form-control" name="state">
                                <option value="0">普通</option>
                                <option value="1">推荐</option>
                            </select>
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary submitBtn">保存</button>
            </div>
        </div>
    </div>
</div>


<script type="text/javascript">
    $(function () {
        $(".inputBtn").click(function () {
            $("#editForm")[0].reset();
            var json = $(this).data("json");
            console.log(json);
            if(json){
                $("#editForm input[name=id]").val(json.id);
                $("#editForm input[name=title]").val(json.title);
                $("#editForm input[name=price]").val(json.price);
                $("#editForm input[name=inventory]").val(json.inventory);
                $("#editForm input[name=description]").val(json.description);
                $("#editForm input[name=coverUrl]").val(json.coverUrl);
                $("#editForm select[name='type.id']").val(json.typeId);
                $("#editForm select[name=state]").val(json.state?"1":"0");
                $("#coverUrl").attr("src",json.coverUrl);
            }
            $("#inputModal").modal("show");
        })
        $(".submitBtn").click(function () {
            $("#editForm").submit();
        })

        $(".deleteBtn").click(function () {
            var id = $(this).data("id");
            console.log(id);
            $.messager.confirm("温馨提示","请再次请确认删除操作",function () {
                $.get("/goods/delete.do" , {id : id} ,function (data) {
                    if (data.success) {
                        $.messager.alert("温馨提示", "操作成功");
                        setTimeout(function () {
                            location.reload();
                        }, 1000)
                    } else {
                        $.messager.alert("温馨提示", data.msg);
                    }

                })
            })
        })

    });
</script>
</body>
</html>